package com.kaishengit.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.GoalProgress;
import com.kaishengit.entity.Task;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserTask;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.UserService;

public class TaskServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private TaskService service=new TaskService();
	private UserService uservice=new UserService();
	private GoalService gservice=new GoalService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String t=request.getParameter("tmothed");
		if(t==null||"".equals(t)){
			toTask(request,response);
		}else if("add".equals(t)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toAdd(request,response);
			} else if("POST".equals(httpMethod)) {
				Add(request,response);
			}
		}else if("edit".equals(t)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toEdit(request,response);
			} else if("POST".equals(httpMethod)) {
				edit(request,response);
			}
		}else if("deltask".equals(t)){
			toTaskdel(request,response);
		}
	}
	
	/**
	 * 删除任务
	 * @throws IOException 
	 * */
	private void toTaskdel(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String taskid=request.getParameter("id");
		String goalid=request.getParameter("goalid");
		service.del(taskid);
		response.sendRedirect("task.jspx?id="+goalid);
		
	}


	/**
	 * 提交所修改的任务
	 * @throws IOException 
	 * */
	

	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String taskid=request.getParameter("id");
		String goalid2=request.getParameter("goalid");
		String name=request.getParameter("name");
		String starttime=request.getParameter("start");
		String endtime=request.getParameter("end");
		String desc=request.getParameter("desc");
		String isImport=request.getParameter("isImport");
		String progress=request.getParameter("progress");
		service.update(taskid,name,starttime,endtime,desc,isImport,progress);
		response.sendRedirect("task.jspx?id="+goalid2);
	}


	/**
	 * 进入到修改任务的页面edit_task.jsp
	 * @throws IOException 
	 * @throws ServletException 
	 * */

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goalid=request.getParameter("goalid");
		String taskid=request.getParameter("id");
		Task task=service.findByTaskId(taskid);
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<User> userlist=uservice.findAllByProjectId(projectid);
		request.setAttribute("goalid",goalid);
		request.setAttribute("task", task);
		request.setAttribute("userList", userlist);
		request.getRequestDispatcher("WEB-INF/views/edit_task.jsp").forward(request, response);
	}




	/**
	 * 提交所添加的任务
	 * @throws IOException 
	 * */
	
	private void Add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String goalid1=request.getParameter("id");
		System.out.println("goalid:::"+goalid1);
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		String name=request.getParameter("name");
		String starttime=request.getParameter("start");
		String endtime=request.getParameter("end");
		String desc=request.getParameter("desc");
		String isImport=request.getParameter("isImport");
		String userid=request.getParameter("manager");
		service.save(goalid1,name,starttime,endtime,desc,isImport,userid,projectid);
		response.sendRedirect("task.jspx?id="+goalid1);
	}
	
	/**
	 * 进入到添加任务的页面new_task.jsp
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		List<User> userlist=uservice.findAllByProjectId(projectid);
		String id=request.getParameter("id");
		request.setAttribute("goalid", id);
		request.setAttribute("userList", userlist);
		request.getRequestDispatcher("WEB-INF/views/new_task.jsp").forward(request, response);
	}




	/**
	 * 进入到任务显示页面task.jsp:
	 * */

	private void toTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goalid=request.getParameter("id");
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		GoalProgress gp=gservice.findByGoalId(goalid, projectid);
		List<UserTask> userTasklist=service.findBygoal(goalid);
		request.setAttribute("userTasklist", userTasklist);
		request.setAttribute("GoalProgress", gp);
		request.getRequestDispatcher("WEB-INF/views/task.jsp").forward(request, response);
	}

}
